<?php
class ome_ctl_admin_goods_sku extends desktop_controller{

    var $workground = 'goods_manager';

    function index(){
        $this->finder('ome_mdl_product_sku_map',array(
            'title'=>'商品SKU',
            'actions'=>array(
                array('label'=>'新建','href'=>'index.php?app=ome&ctl=admin_goods_sku&act=add&finder_id='.$_GET['finder_id'],'target'=>'_blank'),
             ),
            'use_buildin_new_dialog' => false,
            'use_buildin_set_tag'=>false,
            'use_buildin_recycle'=>true,
            'use_buildin_export'=>false,
            'use_buildin_import'=>false,
            'use_buildin_filter'=>true,

             'orderBy' =>'map_id DESC'
        ));
    }

	function add(){
		$shopObj = &$this->app->model("shop");
		$shopData = $shopObj->getList('shop_id,name,node_id');
		$this->pagedata['shopData'] = $shopData;
		
         $this->singlepage('admin/goods/product/edit_sku.html'); 
    }


    function edit($mapid=null){

    	$oSku = &$this->app->model('product_sku_map');
    	$sku =$oSku->dump($mapid);
    	
        $shopObj = &$this->app->model("shop");
        $shopData = $shopObj->getList('shop_id,name,node_id');
        
        
        $this->pagedata['shopData'] = $shopData;
        $this->pagedata['sku'] = $sku;//array('shop_id'=>'9da579eab3662cc0086b89cb65b23198');
        $this->singlepage('admin/goods/product/edit_sku.html'); 
    }


	
	function save() {
		$sku = &$_POST ['sku'];
		$shopid=explode('*', $sku['shop_id']);
		$sku['shop_id']=$shopid[0];
		$sku['node_id']=$shopid[1];
		
		$filter=array(
				//'sku'=>$sku['sku'],
				//'bn'=>$sku['bn'],
				'shop_id'=>$sku['shop_id'],
				'map_id|noequal'=>$sku['map_id'],
				'filter_sql'=>" ( `sku`=". $sku['sku']. " or bn='".$sku['bn']."')", //filter_sql 可以自己写sql语句
				);
		//var_dump($filter);
		$oSku = &$this->app->model ( 'product_sku_map' );
		$this->begin ( 'index.php?app=ome&ctl=admin_goods_sku&act=index' );
		//$sql ="select * from sdb_ome_product_sku_map where shop_id='{$sku['shop_id']}' and map_id<>{$sku['map_id']} ";
		$map = $oSku->dump ( $filter, '*' );
		//var_dump($map);
		if ($map) {
			trigger_error ( app::get ( 'base' )->_ ( '货号或者sku已存在' ), E_USER_ERROR );
			$this->end ( false, app::get ( 'base' )->_ ( '货号或者sku已存在' ) );
		}
		
		$this->end ( $oSku->save ( $sku ), '操作成功' );
	}


}
